package com.hqyj.module.clock.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.module.clock.dao.UserInfoDao;
import com.hqyj.module.clock.pojo.ClockTable;
import com.hqyj.module.clock.pojo.SearchBean;
import com.hqyj.module.clock.service.ClockService;
import com.hqyj.module.holiday.pojo.Holiday;
import com.hqyj.module.index.pojo.DeptTime;
import com.hqyj.module.index.service.DeptTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lap
 * @version 1.0
 * @date 2021/9/29 18:19
 */
@RestController
@RequestMapping("/user-info")
public class ClockController {

    @Autowired
    private ClockService clockService;
    @Autowired
    private DeptTimeService deptTimeService;
    @Autowired
    private UserInfoDao userInfoDao;

    @GetMapping("/getClock")
    public Map<String,Object> getClock(int userId,String nowDay){
        Map<String, Object> clock = clockService.getClock(userId, nowDay);
        return clock;
    }

    @GetMapping("/clock")
    public Map<String,Object> insert(ClockTable clockTable){
        Map<String, Object> map = new HashMap<>();
        String startTime = clockTable.getStartTime();
        String s = startTime.split(" ")[1].split(":")[0];
        String likeTime = clockTable.getUserId().toString().charAt(0)+"%";
        DeptTime deptTime = deptTimeService.listDeptTime(likeTime);
        String workingHour = deptTime.getWorkingHour().split(":")[0];
        if(Integer.parseInt(s) > Integer.parseInt(workingHour)){
            Integer lateAndEarly = userInfoDao.getLateAndEarly(clockTable.getUserId());
            if(lateAndEarly > 0){
                Integer integer = userInfoDao.updateLateAndEarly(clockTable.getUserId());
                if(integer > 0){
                    map.put("late","迟到早退一次");
                }
            }else {
                map.put("late","超过迟到早退次数");
            }
        }
        Map<String, Object> map1 = clockService.insertClock(clockTable);
        if(map1.get("info")!=null){
            map.put("info",map1.get("info"));
        }
        return map;
    }

    @GetMapping("/exitClock")
    public Map<String,Object> updateClock(ClockTable clockTable){
        Map<String, Object> map = new HashMap<>();
        String endTime = clockTable.getEndTime();
        String s = endTime.split(" ")[1].split(":")[0];
        String likeTime = clockTable.getUserId().toString().charAt(0)+"%";
        DeptTime deptTime = deptTimeService.listDeptTime(likeTime);
        String rushHour = deptTime.getRushHour().split(":")[0];
        if(Integer.parseInt(s) < Integer.parseInt(rushHour)) {
            Integer lateAndEarly = userInfoDao.getLateAndEarly(clockTable.getUserId());
            if (lateAndEarly > 0) {
                Integer integer = userInfoDao.updateLateAndEarly(clockTable.getUserId());
                if (integer > 0) {
                    map.put("late", "迟到早退一次");
                }
            } else {
                map.put("late", "超过迟到早退次数");
            }
        }
        Map<String, Object> map1 = clockService.updateClock(clockTable);
        if(map1.get("info")!=null){
            map.put("info",map1.get("info"));
        }
        return map;
    }

    @RequestMapping("/login")
    public String login(){
        return "map";
    }

    /**
     * @author: LAP
     * @description: 分页查询 127.0.0.1:8081/api/cities ---- post
     * @date: 2021/8/19 17:55
     * @param : {"currentPage":1,"pageSize":5,"orderBy":"city_name","direction":"desc","keyword":""}
     */

}
